package com.key.web_admin.controller;

import com.key.common.base.RespResult;
import com.key.common.base.TranException;
import com.key.common.constants.Constants;
import com.key.common.utils.CosUtils;
import com.key.modules.dto.CarrierDTO;
import com.key.modules.dto.CustomerDTO;
import com.key.modules.entity.CarrierContract;
import com.key.modules.entity.CustomerContract;
import com.key.modules.service.CarrierContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.net.URL;

@RestController
@RequestMapping("/carrierContract")
public class CarrierContractController {

    @Autowired
    CarrierContractService service;

    /**
     * 根据条件得到对应条件的承运商合同信息
     * @param carrierDTO
     * @return
     */
    @GetMapping("/showCarrierContract")
    public RespResult showCarrierContract(@RequestBody CarrierDTO carrierDTO){
        return service.showCustomerContract(carrierDTO);
    }

    /**
     * 删除承运商合同，可多个
     * @param ids
     * @return
     */
    @PostMapping("/delCarrierContract")
    public RespResult delCustomerContract(Integer[] ids){
        return service.delCarrierContract(ids);
    }

    /**
     * 修改承运商合同
     * @param contract
     * @return
     */
    @PostMapping("/updateCarrierContract")
    public RespResult updateCarrierContract(@RequestBody CarrierContract contract){
        return service.updateCarrierContract(contract);
    }

    /**
     * 上传文件
     * @param imgUrl
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public RespResult upload(@RequestParam("imgUrl") MultipartFile imgUrl) throws IOException {
        try {
            CosUtils.initCosClient();
            URL url = CosUtils.uploadFile(imgUrl, Constants.BUCKETNAME, CosUtils.formatKey(imgUrl));
            if(!url.toString().equals("")){
                return RespResult.success(url.toString());
            }else {
                throw new TranException("上传失败");
            }
        } finally{
            CosUtils.shutdownClient();
        }
    }

    /**
     * 添加客户合同
     * @param contract
     * @return
     */
    @PostMapping("/addCarrierContract")
    public RespResult addCarrierContract(@RequestBody CarrierContract contract){
        return service.addCarrierContract(contract);
    }

    /**
     * 根据id查询承运商合同信息
     * @param id
     * @return
     */
    @GetMapping ("/showCarrierContractById")
    public RespResult showCarrierContractById(Integer id){
        return service.showCarrierContractById(id);
    }


}
